<?php

namespace App\Http\Controllers\Lost;

use App\Repository\LostRepository;
use App\Repository\UserRepository;
use Endroid\QrCode\QrCode;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

class UserController extends Controller
{

    protected $lost;

    protected $user;

    public function __construct(LostRepository $lost, UserRepository $user)
    {
        $this->lost = $lost;
        $this->user = $user;
    }

    public function index()
    {
        return $this->lost->getAuditByNumber(\Auth::guard('wechat')->user()->number);
    }

    public function qrCode(Request $request)
    {
        $id = \Crypt::decrypt($request->input('id'));

        $loser = $this->user->getUserByNumber($this->lost->find($id)->number);

        if($loser->open_id !== \Auth::guard('wechat')->user()->open_id){
            abort(401);
        }

        $url = route('lost.scan', ['id' => $request->input('id')]);

        $qrCode = new QrCode();

        $qrCode->setText($url);

        return $qrCode->getDataUri();

    }
}
